
package com.trydofor.id.a9id;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

import com.trydofor.id.core.Range;

/**
 * A9id Helper
 * 
 * @author Shi Rongjiu (www.trydofor.com)
 * @since 0.9
 */
@Immutable
public final class A9idHelper {
    
    private A9idHelper() {
    }
    
    /**
     * get the A9ID range by worker and second.
     * 
     * @param worker the minimum worker
     * @param second the minimum second.
     * @return @Nonnull the Range
     */
    @Nonnull
    public static Range getRange(int worker, long second) {
        A9ID min = new A9ID(worker, second, (int) A9ID.getRangeOfSerial().minimum());
        long maxSecond = A9ID.getRangeOfSecond().maximum();
        int maxSerial = (int) A9ID.getRangeOfSerial().maximum();
        A9ID max = new A9ID(worker, maxSecond, maxSerial);
        return new Range(min.longValue(), max.longValue());
    }
    
    /**
     * get the offset between workers.
     * a9id(new worker) = a9id(old worker) + offset.
     * 
     * @param worker the candidate worker
     * @param toWorker new worker
     * @return offset
     */
    public static long getRangeOffset(int worker, int toWorker) {
        return (toWorker - worker) * (1L << (A9ID.getBitOfSecond() + A9ID.getBitOfSerial()));
    }
}
